/*
 *@Description: 素材菜单统一接口调用处理
 *@MethodAuthor:  shenmengfei_sinosoft
 *@param {
    pagePwd：菜单标识 [文章：'Article', 海报：'Post', 多媒体：'Media', 话术：'Language', 险种：'Insurance', 文件：'File', 链接：'Link', 问卷：'Form', ]
    operate：分组操作 [查询：'QUERY', 删除：'DEL', 新增：'ADD', 编辑：'EDIT']
    params：入参
  }
 *@returns { response：调用结果 }
 *@Date: 2023-06-30 10:56:02
*/

import { categoryAddCorp, categoryAddPer, categoryDelCorp, categoryDelPersonal, categoryEditCorp, categoryEditPer, categoryList } from '@/api/article/group';

function handleQuery(pagePwd, params) {
  return new Promise(function (resolve, reject) {
    // 0：企业库；1：个人库
    categoryList(params).then((res) => {
      if (res.code === 200) {
        resolve(res.data || [])
      }
    }).catch(() => {
      reject('ERROR')
    })
  })
}
function handleDel(pagePwd, currentTab, params) {
  return new Promise(function (resolve, reject) {
    // 0：企业库；1：个人库
    let api = currentTab == '0' ? categoryDelCorp : categoryDelPersonal
    api(params).then((res) => {
      if (res.code === 200) {
        resolve('SUCCESS')
      }
    }).catch(() => {
      reject('ERROR')
    })
  })
}
function handleAdd(pagePwd, currentTab, params) {
  return new Promise(function (resolve, reject) {
    // 0：企业库；1：个人库
    let api = currentTab == '0' ? categoryAddCorp : categoryAddPer
    api(params).then((res) => {
      if (res.code === 200) {
        resolve('SUCCESS')
      }
    }).catch(() => {
      reject('ERROR')
    })
  })
}
function handleEdit(pagePwd, currentTab, params) {
  return new Promise(function (resolve, reject) {
    // 0：企业库；1：个人库
    let api = currentTab == '0' ? categoryEditCorp : categoryEditPer
    api(params).then((res) => {
      if (res.code === 200) {
        resolve('SUCCESS')
      }
    }).catch(() => {
      reject('ERROR')
    })
  })
}

export function meterialDoneApi(pagePwd, currentTab, operate, params) {
  return new Promise(function (resolve, reject) {
    if (operate === 'QUERY') {
      resolve(handleQuery(pagePwd, params));
    } else if (operate === 'DEL') {
      resolve(handleDel(pagePwd, currentTab, params));
    } else if (operate === 'ADD') {
      resolve(handleAdd(pagePwd, currentTab, params));
    } else if (operate === 'EDIT') {
      resolve(handleEdit(pagePwd, currentTab, params));
    }
  })
}
